package com.operate.task;

import com.alibaba.fastjson2.JSONObject;
import com.operate.bean.VO.Fields;
import com.operate.bean.VO.OnlineOrderNumVO;
import com.operate.config.HttpConfig;
import com.operate.service.ScreenDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: ZeckZheng
 * @time: 2023/11/2 5:25
 */
@Slf4j
@Service
public class OnlineOrderNumTask {

    @Value("${order.enable}")
    private boolean enable;

    @Value("${order.query.url}")
    private String query_url;

    @Value("${order.delete.url}")
    private String delete_url;

    @Value("${order.insert.url}")
    private String insert_url;

    @Autowired
    private HttpConfig httpConfig;


    @Autowired
    private ScreenDataService service;


    //@Scheduled(cron = "${order.cron}")
    @Async("myAsync")
    public void onlineOrderTask() {
        if (enable == false) {
            log.info("OnlineOrder Task 未开启");
            return;
        }
        List<OnlineOrderNumVO> orderList = service.onlineOrderNumList();
        httpConfig.clearData(query_url, delete_url);

        if (orderList != null && !orderList.isEmpty()) {

            List<Fields> insertList = new ArrayList<>();
            for (OnlineOrderNumVO vo : orderList) {
                Fields fields = new Fields(vo);
                insertList.add(fields);
            }
            Map<String, List<Fields>> insertMap = new HashMap<>();
            insertMap.put("records", insertList);

            String insertResult = httpConfig.POST(insert_url, JSONObject.toJSONString(insertMap));

        }
    }
}

